#!/usr/bin/python
# coding:utf-8
__author__ = 'good'
__createday__ = '2015-01-15'

from datetime import datetime

from golsee import db


class Login_log(db.Model):
    """登陆日志"""
    id = db.Column(db.Integer, primary_key=True)
    email = db.Column(db.String(100), index=True)
    timestamp = db.Column(db.DateTime, default=datetime.now())
    ip = db.Column(db.String(15))
    address = db.Column(db.String(50))

    def __init__(self, email, ip):
        self.email = email
        self.ip = ip
        self.address = self.get_ip_location(ip)

    def __str__(self):
        return self.email

    @classmethod
    def count_all(cls):
        count = db.session.query(db.func.count(Login_log.id).label('login_all')).first().login_all
        return count

    #: 返回IP地理位置
    def get_ip_location(self, ip):
        import urllib2
        import json

        try:
            url = 'http://ip.taobao.com/service/getIpInfo.php?ip=' + ip
            js = urllib2.urlopen(url).read()
        except:
            return u'查询接口无法打开'
        s = json.loads(js)
        if not s['code']:
            return s['data']['country'] + s['data']['area'] + s['data']['region'] + s['data']['city'] + s['data']['isp']
        else:
            return u'查询失败'


class Visit_log(db.Model):
    """备份日志"""
    __tablename__ = 'visit_log'
    id = db.Column(db.Integer, primary_key=True, index=True)
    timestamp = db.Column(db.DateTime, default=datetime.now())
    ip = db.Column(db.String(15))
    url = db.Column(db.String(100))
    agent = db.Column(db.String(500))

    def __repr__(self):
        return '<URL: %r>' % (self.url)

    def __init__(self, timestamp, ip, url, agent):
        self.timestamp = timestamp
        self.ip = ip
        self.url = url
        self.agent = agent

    @classmethod
    def count_all(cls):  # 访问总量
        count = db.session.query(db.func.count(Visit_log.id).label('visit_all')).first().visit_all
        return count

    @classmethod
    def max_id(cls):  #
        return db.session.query(db.func.max(Visit_log.id).label('max_id')).first().max_id


class Backup_log(db.Model):
    """备份日志"""
    __tablename__ = 'backup_log'
    id = db.Column(db.Integer, primary_key=True)
    start_time = db.Column(db.DateTime)
    finish_time = db.Column(db.DateTime)
    status = db.Column(db.String(10))
    msg = db.Column(db.Text)
    type = db.Column(db.String(10))


class Ip_blacklist(db.Model):
    """ip黑名单"""
    __tablename = 'ip_blacklist'
    ip = db.Column(db.String(15), primary_key=True, index=True)
    address = db.Column(db.String(50))
    visit_count = db.Column(db.Integer, default=1)
    attack_count = db.Column(db.Integer, default=1)
    is_forbid = db.Column(db.Integer, default=0)  # 是否禁止访问
    forbid_date = db.Column(db.Integer)  # 禁止日期
    reason = db.Column(db.String(50))  # 原因

    @classmethod
    def find_by_ip(cls, ip):
        ip = Ip_blacklist.query.filter_by(ip=ip).first()
        return ip


class Robot(db.Model):
    """机器人清单"""
    __tablename__ = 'robot'
    ip = db.Column(db.String(15), primary_key=True, index=True)
    name = db.Column(db.String(20))
    dns_name = db.Column(db.String(20))
    address = db.Column(db.String(50))